package me.helllp.bootman.core.factory;

import java.util.ArrayList;
import java.util.List;

import me.helllp.bootman.core.bean.MethodBean;
import me.helllp.bootman.core.bean.VoBean;
import me.helllp.bootman.core.service.FactoryService;

public class ModuleFacotryBuilder {
	private String packageName;
	private String memo;
	private String name;
	private String rootUrl;
	
	private class MethodInfo{
		public MethodBean method;
		public List<VoBean> input;
		public List<VoBean> output;
		
		public MethodInfo(MethodBean method,List<VoBean> input,List<VoBean> output){
			this.method = method;
			this.input = input;
			this.output = output;
		}
	}
	
	private List<MethodInfo> nodeList = new ArrayList<MethodInfo>();
	
	public ModuleFacotryBuilder setPackageName(String packageName){
		this.packageName = packageName;
		return this;
	}
	
	public ModuleFacotryBuilder setMemo(String memo){
		this.memo = memo;
		return this;
	}
	
	public ModuleFacotryBuilder setName(String name){
		this.name = name;
		return this;
	}

	public ModuleFacotryBuilder setRootUrl(String url){
		this.rootUrl = url;
		return this;
	}
	
	public ModuleFacotryBuilder addMethod(MethodBean method,List<VoBean> input,List<VoBean> output){
		nodeList.add(new MethodInfo(method, input, output));
		return this;
	}
	
	public FactoryService build(){
		List<VoFactory> voFacList = new ArrayList<VoFactory>();
		
		ControllerFactory controllerFacotry = new ControllerFactory(name,packageName);
		ServiceFactory serviceFacotry = new ServiceFactory(name,packageName);
		ServiceImplFactory serviceImplFactory = new ServiceImplFactory(name,packageName);
		
		serviceFacotry.setNodeList(new ArrayList<MethodBean>());
		serviceFacotry.setPackageName(packageName + ".services");
		
		serviceImplFactory.setNodeList(serviceFacotry.getNodeList());
		serviceImplFactory.setPackageName(packageName + ".services.impl");
		
		controllerFacotry.setMemo(memo);
		controllerFacotry.setNodeList(serviceFacotry.getNodeList());
		controllerFacotry.setPackageName(packageName + ".web");
		controllerFacotry.setRootUrl(rootUrl);
		controllerFacotry.setServiceImpl(serviceFacotry.getPackageName() + "." + serviceFacotry.getIdentify() + "Service");
		
		
		for(MethodInfo info : nodeList){
			if(info.input != null){
				voFacList.add(createVoFactory(info.method, info.input, true));
			}
			
			if(info.output != null){
				voFacList.add(createVoFactory(info.method, info.output, false));
			}

			serviceFacotry.getNodeList().add(info.method);
		}
		
		ModelFactory factory = new ModelFactory(controllerFacotry,serviceFacotry,serviceImplFactory,voFacList);
		
		return factory;
	}

	private VoFactory createVoFactory(MethodBean method, List<VoBean> input, boolean isInput) {
		VoFactory voFac =  new VoFactory(method.getMethodName() + (isInput ? "Input" : "Output"),packageName);
		
		voFac.setMemo(method.getMemo());
		voFac.setNodeList(input);
		voFac.setPackageName(packageName + ".vo");
		
		return voFac;
	}
}
